package com.szholly.plug.form.entity;

import java.util.HashMap;

import org.springframework.stereotype.Component;

import com.szholly.utils.spring.SpringBeanFactory;
import com.szholly.data.common.util.TablesUtils;
import com.szholly.data.general.Field;
import com.szholly.data.general.GlobalDataType;
import com.szholly.data.general.IDomain;
import com.szholly.data.general.InterceptorTableBase;
import com.szholly.data.general.MemDomain;
import com.szholly.data.general.RelationInfo;
import com.szholly.data.general.RelationNode;
import com.szholly.data.general.RelationStrength;
import com.szholly.data.general.RelationType;
import com.szholly.data.general.RowBase;
import com.szholly.data.general.metadata.IBaseTable;

/**
 * 定义表单的主信息
 */
@Component("FORM_FORMINFO")
public class FormInfoTable extends InterceptorTableBase implements IBaseTable {
	public final static String TABLE_NAME = "FORM_FORMINFO";

	/**
	 * 获取单例对象
	 */
	public static FormInfoTable getSingleRef() {
		return (FormInfoTable) SpringBeanFactory
				.getBean("FORM_FORMINFO");
	}
	
	@Override
	public String getMetadataTableName() {
		return TABLE_NAME;
	}

	@Override
	public String getMetadataTableAliasName() {
		return "表单的主信息表";
	}
	
	@Override
	public String getTableGroup() {
		return "表单信息";
	}
	
	@Override
	public String getGUIDFieldName(){
		return RowBase.FIELD_GUID;
	}
	
	private HashMap<String, Field> fields;

	@Override
	protected HashMap<String, Field> fields() {
		if (fields == null) {
			fields = new HashMap<String, Field>();

			Field itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_GUID);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(1);
			itemField.setFieldAliasName("全局ID");
			itemField.setCanShow(false);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(RowBase.FIELD_VERSION);
			itemField.setDataType(GlobalDataType.Int32);
			itemField.setFieldID(2);
			itemField.setFieldAliasName("数据版本");
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(FormInfoRow.PROP_FORMID);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(3);
			itemField.setFieldAliasName("表单ID");
			itemField.setIsKey(true);
			itemField.setCanShow(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(FormInfoRow.PROP_FORMNAME);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(4);
			itemField.setFieldAliasName("表单名称");
			itemField.setCanShow(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(FormInfoRow.PROP_SHORTFORMNAME);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(4);
			itemField.setFieldAliasName("表单简称");
			itemField.setCanShow(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(FormInfoRow.PROP_TABLENAME);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(4);
			itemField.setFieldAliasName("表单所使用的数据表名称");
			itemField.setCanShow(true);
			HashMap<String, String>	codes = TablesUtils.getTablesMap();
			IDomain pDomain = new MemDomain(codes, "(元数据)元数据表");
			itemField.setDomain(pDomain);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(FormInfoRow.PROP_EDITFLP);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(4);
			itemField.setFieldAliasName("表单编辑模板");
			itemField.setCanShow(true);
			itemField.setCodeTablePK(FormModelTable.TABLE_NAME);
			itemField.setCodeDomainName("");
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(FormInfoRow.PROP_FORMPATH);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(4);
			itemField.setFieldAliasName("表单访问路径");
			itemField.setCanShow(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(FormInfoRow.PROP_VIEWFLP);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(4);
			itemField.setFieldAliasName("表单展示模板");
			itemField.setCodeTablePK(FormModelTable.TABLE_NAME);
			itemField.setCodeDomainName("");
			itemField.setCanShow(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(FormInfoRow.PROP_FORMGROUP);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(4);
			itemField.setFieldAliasName("表单分组");
			itemField.setCanShow(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(FormInfoRow.PROP_FORMORDER);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(4);
			itemField.setFieldAliasName("表单排序对象");
			itemField.setCanShow(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(FormInfoRow.PROP_FORMDEFULAT);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(4);
			itemField.setFieldAliasName("表单默认值");
			itemField.setCanShow(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(FormInfoRow.PROP_FORMQUERY);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(4);
			itemField.setFieldAliasName("表单默认查询对象");
			itemField.setCanShow(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(FormInfoRow.PROP_FORMPAGESIZE);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(4);
			itemField.setFieldAliasName("表单分页大小");
			itemField.setCanShow(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
			
			itemField = new Field();
			itemField.setFieldName(FormInfoRow.PROP_FORMBPM);
			itemField.setDataType(GlobalDataType.String);
			itemField.setFieldID(4);
			itemField.setFieldAliasName("表单绑定的工作流ID");
			itemField.setCanShow(true);
			fields.put(itemField.getFieldName().toUpperCase().trim(), itemField);
		}
		return fields;
	}
	
	private RelationNode relationNode;
	/**
	 * 表关系
	 */
	@Override
	public RelationNode getRelation() {
		if (relationNode == null) {
			relationNode = new RelationNode(TABLE_NAME);

			RelationInfo relationInfo = new RelationInfo();
			relationInfo.Foreignkey = new HashMap<String, String>();
			relationInfo.Foreignkey.put(FormInfoRow.FIELD_GUID, FormFieldRow.PROP_FORMID);
			relationInfo.RelationStrength = RelationStrength.UpdateChild;
			relationInfo.RelationType = RelationType.OneToMore;
			relationInfo.metadataTableAliasName = "数据录入表";
			relationNode.getChildRelationNode().put(FormFieldTable.TABLE_NAME, relationInfo);
		}
		return relationNode;
	}
}